package com.koron.standard.orderCard.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.koron.bean.base.Response;
import com.koron.standard.bean.query.PageQuery;
import com.koron.standard.orderCard.bean.OrderCard;
import com.koron.standard.orderCard.bean.OrderCardQuery;
import com.koron.standard.orderCard.feign.DeviceFeignService;
import com.koron.standard.orderCard.feign.OrderFeignService;
import com.koron.standard.orderCard.service.OrderCardServiceWarpper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/ZsjRemote")
@RestController
@Api(tags = "珠三角远程协作")
public class OrderCardController {

	@Autowired
	private OrderCardServiceWarpper serviceWarpper;

	@Autowired
	private DeviceFeignService deviceFeignService;

	@Autowired
	private OrderFeignService orderFeignService;

	@GetMapping("/queryOrderCard")
	@ApiOperation(value = "获取工卡列表")
	public Response<Page<OrderCard>> queryOrderCard(OrderCardQuery orderCardQuery,
	                                                PageQuery<OrderCard> pageQuery,
	                                                @RequestParam(required = false,defaultValue = "1") Integer isPage){
		Page<OrderCard> orderCards = serviceWarpper.queryOrderCard(orderCardQuery,pageQuery,isPage);
		return Response.success("查询成功",orderCards);
	}

	@GetMapping("/getOrderCardById")
	@ApiOperation(value = "根据工卡id,编码等方式获取工卡详情")
	public Response<OrderCard> getOrderCardById(String id, String code){
		return  Response.succ(serviceWarpper.getOrderCardById(id,code));
	}

	@GetMapping("/getOrderCardByOrderId")
	@ApiOperation(value = "根据工单id获取工卡配置信息")
	public Response<List<OrderCard>> getOrderCardByOrderId(String id,
	                                                       PageQuery pageQuery,
	                                                       @RequestParam(required = false,defaultValue = "0") Integer isPage){
		String typeId = orderFeignService.getDeviceTypeByOrderId(id);
		return Response.succ(serviceWarpper.queryByTypeId(typeId));
	}


	@GetMapping("/queryOrderCardByKKS")
	@ApiOperation(value = "根据设备KKS编码获取工卡列表(有不同方案)")
	public Response<List<OrderCard>> queryOrderCardByKKS(String encode){
		String typeId = deviceFeignService.getTypeByKKS(encode);
		return Response.succ(serviceWarpper.queryByTypeId(typeId));
	}

	@PostMapping("/saveOrUpdateOrderCard")
	@ApiOperation(value = "添加或更改工单详情")
	public Response saveOrUpdateOrderCard(@RequestBody OrderCard orderCard){
		return serviceWarpper.saveOrUpdateOrderCard(orderCard);
	}

	//此处删除工卡仅改变delete_flag值,没有删除数据库中的数据
	@PostMapping("/deleteOrderCardById")
	@ApiOperation(value = "删除工卡")
	public Response deleteOrderCardById(String orderCardId){
		return serviceWarpper.deleteOrderCardById(orderCardId);
	}

	//使用工具的查询将连接表中的数据直接删除,但不改变工卡主表和工具主表
	@PostMapping("/deleteToolById")
	@ApiOperation(value = "删除使用工具")
	public Response deleteToolById(@RequestBody List<String> toolIds){
		return serviceWarpper.deleteToolById(toolIds);
	}

	/*
	接口只删除步骤表中的数据,与之关联的工卡主表,附件主表不予删除
	步骤里面的附件由前端调用附件删除接口进行删除
	*/
	@PostMapping("/deleteStepById")
	@ApiOperation(value = "删除步骤")
	public Response deleteStepById(@RequestBody List<String> stepIds){
		return serviceWarpper.deleteStepById(stepIds);
	}

	@PostMapping("/deleteSingleFileById")
	@ApiOperation(value = "删除单个文件")
	public Response deleteSingleFileById(String id){
		return serviceWarpper.deleteSingleFileById(id);
	}

	@PostMapping("/deleteFilesById")
	@ApiOperation(value = "删除单个文件")
	public Response deleteFilesById(@RequestBody List<String> ids){
		return serviceWarpper.deleteFilesById(ids);
	}
}

